/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import alloy.type.UnionType;

/**
 * Alloy Analyzer
 * Copyright (c) 2001
 * Massachusetts Institute of Technology
 *
 * Class: ArrowMultExpr
 * owner: brianlin / msridhar / emina
 * Interface status: unstable
 * Implementation status: unstable
 *
 * AST node representing a/an ArrowMultExpr:  Multiplicity MultiplicityExpr -> MultiplicityExpr
 */

public class ArrowMultExpr extends TreeNode implements MultiplicityExpr {

    /**
     * index of set mult in child list
     */
    private static final int MULT_INDEX = 0;


    /**
     * index of left in child list
     */
    private static final int LEFT_INDEX = 1;

    /**
     * index of right in child list
     */
    private static final int RIGHT_INDEX = 2;



    /** the {@link UnionType} of this expression */
    private UnionType _type;

    public ArrowMultExpr(Location location_, Multiplicity mult_, MultiplicityExpr leftExpr_, 
			 MultiplicityExpr rightExpr_) {
        super(location_, mult_, leftExpr_, rightExpr_);
    }


    public ArrowMultExpr(Multiplicity mult_, MultiplicityExpr leftExpr_, MultiplicityExpr rightExpr_) {
        super(Location.UNKNOWN, mult_, leftExpr_, rightExpr_);
    }



    //GET AND SET METHODS

    /**
     * gets the left of this
     * @return the {@link MultiplicityExpr}
     */
    public MultiplicityExpr getLeft() {
        return (MultiplicityExpr)childAt(LEFT_INDEX);
    }

    /** set the Left of this */
    public void setLeft(MultiplicityExpr newChild_) {
        setChild(LEFT_INDEX, newChild_);
    }

    /**
     * gets the right of this
     * @return the {@link Expr}
     */
    public MultiplicityExpr getRight() {
        return (MultiplicityExpr)childAt(RIGHT_INDEX);
    }

    /** set the Right of this */
    public void setRight(MultiplicityExpr newChild_) {
        setChild(RIGHT_INDEX, newChild_);
    }

    public Expr toExpr() {
        TypedExpr ret = new BinaryExpr(getLeft().toExpr(), 
				       BinaryExprOp.RELATION, 
				       getRight().toExpr());
        if (getLeft().getType() != null &&
            getRight().getType() != null) {
        	ret.setType(getLeft().getType().product(getRight().getType()));
        }
        return (Expr)ret.copy();
    }
                                        

    /**
     * gets the mult of this
     * @return the {@link RelMultiplicity}
     */
    public Multiplicity getMult() {
        return (Multiplicity)childAt(MULT_INDEX);
    }

    /** set the mult of this */
    public void setMult(Multiplicity newChild_) {
        setChild(MULT_INDEX, newChild_);
    }


    /**
     * get the type of this
     * @return the type of this
     */
    public UnionType getType() {
        return _type;
    }
    
    /**
     * set the type of this
     * @param type_ new type for this
     */
    public void setType(UnionType type_) {
        _type = type_;
        if (type_ != null) {
            descr = "type " + type_.toString();
        } else {
            descr = "";
        }
    }

    public boolean isSameAs(MultiplicityExpr multExpr_) {
	if (!multExpr_.getClass().equals(getClass())) return false;
	ArrowMultExpr arrowMultExpr_ = (ArrowMultExpr)multExpr_;
	return
	    _type.equals(arrowMultExpr_._type) &&
	    getMult().equals(arrowMultExpr_.getMult()) &&
	    getLeft().getMult().equals(arrowMultExpr_.getLeft().getMult()) &&
	    getRight().getMult().equals(arrowMultExpr_.getRight().getMult());
    }

    public Object copy() {
        ArrowMultExpr exprCopy = (ArrowMultExpr)super.copy();
        exprCopy.setType(_type);
        return exprCopy;
    }
    
    /**
     * accept an AST visitor
     * @param visitor_ the visitor
     */
    protected void acceptVisitor(ASTVisitor visitor_) {
        visitor_.visit(this);
    }

    /**
     * accept a returning AST visitor
     * @param visitor_ the visitor
     * @return the Object returned by the visitor
     */
    protected Object acceptReturnVisitor(ASTReturnVisitor visitor_) {
        return visitor_.visit(this);
    }

    /**
     * If exprIndex == RIGHT_INDEX, returns the String representation
     * of multExpr_ as "Mult (expr of multExpr)."
     * If exprIndex == LEFT_INDEX, returns the String "(expr of multExpr) Mult"
     **/
    private StringBuffer childString(int exprIndex, MultiplicityExpr multExpr) {
	StringBuffer sb = new StringBuffer();

	Multiplicity mult = multExpr.getMult();

	multExpr.setMult(Multiplicity.NONE);

	sb.append("(");
	sb.append(multExpr.nodeString());
	sb.append(")");

	if (exprIndex == LEFT_INDEX) {
	    sb.append(" ");
	    sb.append(mult.nodeString());
	} else if (exprIndex == RIGHT_INDEX) {
	    sb.insert(0," ");
	    sb.insert(0,mult.nodeString());
	}

        multExpr.setMult(mult);

	return sb;
    }

    public String nodeString() {
	StringBuffer sb = new StringBuffer();
	if (getMult().equals(Multiplicity.NONE)) {
	    sb.append(getMult().nodeString());
	    sb.append(" ");
	}

	sb.append(childString(LEFT_INDEX, getLeft()));
	
	sb.append("->");
	
	sb.append(childString(RIGHT_INDEX, getRight()));
	

	return sb.toString();
    }

    public String toString()
    {
        return nodeString();
    }
}
